
package Test;

import java.awt.Color;
import java.io.File;

import junit.framework.TestCase;
import svgedit.gui.Frame;
import svgedit.svg.SVGCircleElement;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGPaint;

/**
 *  Test case when change an element stroke color and test when undo this execution
 *
 */
public class ChangeElementTest extends TestCase {

	/**
	 *  Get Frame object
	 * */
	private static Frame frame;
	/**
	 *  Choose a circle element which is going to be changed
	 * */
	private SVGCircleElement selectedElem;
	
	/**
	 *   Backup the selected element stroke color
	 * */
	private SVGPaint stroke;
	
	
	/**
	 *   Constructor for change Element test
	 * */
	public ChangeElementTest(String arg0) {
		super(arg0);
		frame = new Frame();
		File file = new File("src/testfile/test.svg");
        frame.openFile(file);
        selectedElem = getSelectedElement();
        stroke = selectedElem.getStroke();
	}
	

	/**
	 *   Get a circle element in this file
	 *   
	 *   @return elem to get the selected item
	 * */
	public SVGCircleElement getSelectedElement(){
		for (SVGElement elem: frame.getDocument()){
			if (elem instanceof SVGCircleElement && elem.getParent() == frame.getDocument().getRootGroup()){
				return (SVGCircleElement)elem;
			}
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();	
	}
	
	/**
	 *  Test unit when change an element stroke color
	 * */
	public void testChangeElementTest(){
		ChangeElement(selectedElem);
	}
	
	/**
	 *  Test unit when undo changing execution
	 * */
	public void testUndoChangeElementTest(){
		UndoChangeElement(selectedElem);
	}
	
	/**
	 *  Change the selected item stroke color to its original color
	 *  
	 *  @param element the selected element
	 * */
	public void UndoChangeElement(SVGCircleElement element){
		element.getStroke().setValueFromPaint(stroke);
		assertEquals(stroke,element.getStroke());
	}
	
	/**
	 *  Change the selected item stroke color to a new color
	 *  
	 *  @param element the selected element
	 * */
	public void ChangeElement(SVGCircleElement element){
		SVGPaint changePaint = new SVGPaint();
		changePaint.setRGBColor(Color.red);
		element.getStroke().setValueFromPaint(changePaint);
		assertEquals(changePaint,element.getStroke());
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
